import { getFabricDocument } from 'fabric';
 
export const createCanvasElement = () => {
  const element = getFabricDocument().createElement('canvas');
  if (!element || typeof element.getContext === 'undefined') {
    throw new Error('Failed to create `canvas` element');
  }
  return element;
};


export const createImage = () =>
  getFabricDocument().createElement('img');


export const copyCanvasElement = (canvas) => {
  const newCanvas = createCanvasElement();
  newCanvas.width = canvas.width;
  newCanvas.height = canvas.height;
  newCanvas.getContext('2d')?.drawImage(canvas, 0, 0);
  return newCanvas;
};


export const toDataURL = (
  canvasEl,
  format,
  quality
) => canvasEl.toDataURL(`image/${format}`, quality);

export const isHTMLCanvas = (
  canvas
) => {
  return !!canvas && canvas.getContext !== undefined;
};
